package groupon.com.bo.controller;

import groupon.com.util.BoNavigation;
import groupon.com.util.ViewConstants;
import groupon.user.bo.service.ManagerUserService;
import groupon.user.model.User;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
@RequestMapping("/bo")
public class BoController extends BoNavigation{
		
		private final Logger log = Logger.getLogger(BoController.class);
		
		@Autowired
		private ManagerUserService _managerUserService;
		
		@RequestMapping(value = "/manager", method = RequestMethod.GET)
		public String getBoPage(ModelMap model,HttpServletRequest request){
			List<User> users = new ArrayList<User>();
			int pageItems = 5;
			if(request.getParameter("pager.offset") != null){
				users = _managerUserService.findInRange(Integer.parseInt(request.getParameter("pager.offset")) , pageItems);
			}else{
				users = _managerUserService.searchAll();
				request.getSession().setAttribute("totalSize", users.size());
			}
			
			if(request.getSession().getAttribute("totalSize") == null){
				model.addAttribute("size", users.size());
			}else{
				model.addAttribute("size", request.getSession().getAttribute("totalSize"));
			}
			model.addAttribute("pageItems", pageItems);
			model.addAttribute("users", users);
			model.addAttribute("subTab", "createContent");
			
			return ViewConstants.RETURN_MANAGER_PAGE;
		}
		
		@RequestMapping(value="/common", method = RequestMethod.GET)
		public String getCommonPage(){
			log.info("request to this common page ");
			return ViewConstants.RETURN_MANAGER_PAGE;
		}

		@Override
		public String getSubTab() {
			return "";
		}

		@Override
		public String getTab() {
			return "Content";
		}

}
